/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.muni.fi.pa165.mushroom.website.web;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontValidate;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.HandlesEvent;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.validation.Validate;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;

import org.slf4j.LoggerFactory;


/**
 * Bean for logout of user from realm
 * 
 * @author Roman Jakubco <roman.jakubco@gmail.com>
 */
public class UserActionBean extends BasicActionBean{
    private static final transient Logger log = LoggerFactory.getLogger(UserActionBean.class);
    
    @Validate(on = {"login"}, required = true)
    private String username;
    @Validate(on = {"login"}, required = true)
    private String password;
    
    private String message;
   

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @DefaultHandler
    @DontValidate
    public Resolution defaultHander() {
        return new ForwardResolution("/login.jsp");    }

    
    @HandlesEvent("logout")
    public Resolution logout(){
        Subject currentUser = SecurityUtils.getSubject();       
        
        if (currentUser.isAuthenticated()) {
            message = currentUser.toString();         
            
            currentUser.logout();
            return new ForwardResolution("/index.jsp");
        }
        return new ForwardResolution("/index.jsp");
    }

   
    
}
